package eiisan.util.common;

import com.google.common.base.Preconditions;

/**
 * 字符串处理工具类
 * @author gragonfly
 * @date 2019/7/6
 **/
public class StrUtil {
    /**
     * 批量拼接字符串
     */
    public static String concat(String... strings) {
        Preconditions.checkArgument(EmptyUtil.no(strings), "it isn't suitable for empty string.");
        int stringsLength = strings.length;
        if (stringsLength == 1) {
            Preconditions.checkArgument(strings[0] != null, "the first string of the string array is null.");
            return strings[0];
        }
        int size = 0;
        for (int i = 0; i < stringsLength; i++) {
            //如果字符串数组某处为null,会自动抛出异常
            size = size + strings[i].length();
        }
        StringBuilder builder = new StringBuilder(size);
        for (int i = 0; i < stringsLength; i++) {
            builder.append(strings[i]);
        }
        return builder.toString();
    }

    /**
     * 首字母大写
     */
    public static String upperFirst(String string) {
        Preconditions.checkArgument(EmptyUtil.no(string), "it isn't suitable for empty string.");
        return new StringBuilder(string.length()).append(Character.toUpperCase(string.charAt(0))).append(string
                .substring(1)).toString();
    }

    /**
     * 首字母小写
     */
    public static String lowerFirst(String string) {
        Preconditions.checkArgument(EmptyUtil.no(string), "it isn't suitable for empty string.");
        return new StringBuilder(string.length()).append(Character.toLowerCase(string.charAt(0))).append(string
                .substring(1)).toString();
    }

    /**
     * 转换为驼峰命名法<br>
     * 默认以"-"为分隔符,将首字母变为小写,将之后的每个分词的首字母变为大写。如果分隔符无效,则会将首字母变为小写。<br>
     */
    public static String toCamelCase(String string) {
        return toCamelCase("-", string);
    }

    /**
     * 转换为驼峰命名法<br>
     * 以设定的分隔符作为标准,将首字母变为小写,将之后的每个分词的首字母变为大写。如果分隔符无效,则会将首字母变为小写。<br>
     */
    public static String toCamelCase(String delimiter, String string) {
        Preconditions.checkArgument(EmptyUtil.no(string) || EmptyUtil.no(delimiter), "when string is null, delimiter can't be null either.");
        String[] tokens = string.split(delimiter);
        int tokensLength = tokens.length;
        if (tokensLength <= 1) {
            return new StringBuilder(string.length()).append(Character.toLowerCase(string.charAt(0))).append(string
                    .substring(1)).toString();
        }
        char[] upperStarters = new char[tokensLength - 1];
        for (int i = 1; i < tokensLength; i++) {
            upperStarters[i - 1] = Character.toUpperCase(tokens[i].charAt(0));
            tokens[i] = tokens[i].substring(1);
        }
        StringBuilder builder = new StringBuilder(string.length());
        builder.append(Character.toLowerCase(tokens[0].charAt(0)));
        builder.append(tokens[0].substring(1));
        for (int i = 1; i < tokensLength; i++) {
            builder.append(upperStarters[i]).append(tokens[i]);
        }
        return builder.toString();
    }
}
